<?php
    class Game extends CActiveRecord
    {
        public static function model($className = __CLASS__) {
            return parent::model ( $className );
        }

        public function tableName() {                  
            return 'c_game';
        }

        public function getGameOnline($currentPage,$numberRecordPerPage)
        {
            $cacheService = new CacheService("Game","getGameOnline",$currentPage.$numberRecordPerPage);
            $key = $cacheService->createKey();
            $cache = Yii::app()->cache->get($key);
            if ($cache == false) 
            {
                $startRecord = ($currentPage - 1) * $numberRecordPerPage;
                $sql = "SELECT id, title,introtext, alias,picture,download,create_date FROM c_game WHERE  isOnline= 1 AND status=1 ORDER BY create_date DESC LIMIT " . $startRecord . ", " . $numberRecordPerPage;
                $command = Yii::app()->db->createCommand($sql);
                $rows = $command->queryAll(); 
                Yii::app()->cache->set($key, $rows, ConstantsUtil::TIME_CACHE_7200);  
            }else{
                $rows=$cache;
            }  
            return $rows;
        }
        public function getGameOnlineHot($currentPage,$numberRecordPerPage)
        {
            $startRecord = ($currentPage - 1) * $numberRecordPerPage;
            $sql = "SELECT id, title,introtext, alias,picture,download,create_date FROM c_game WHERE  isOnline= 1 AND status=1 ORDER BY download DESC LIMIT " . $startRecord . ", " . $numberRecordPerPage;
            $command = Yii::app()->db->createCommand($sql);
            $rows = $command->queryAll();   
            return $rows;
        }

        public function getGameOffline($currentPage,$numberRecordPerPage)
        {
            $cacheService = new CacheService("Game","getGameOffline",$currentPage.$numberRecordPerPage);
            $key = $cacheService->createKey();
            $cache = Yii::app()->cache->get($key);
            if ($cache == false) 
            {
                $startRecord = ($currentPage - 1) * $numberRecordPerPage;
                $sql = "SELECT id, title,introtext, alias,picture,download,create_date FROM c_game WHERE  isOnline= 0 AND status=1 ORDER BY create_date DESC LIMIT " . $startRecord . ", " . $numberRecordPerPage;
                $command = Yii::app()->db->createCommand($sql);
                $rows = $command->queryAll();
                Yii::app()->cache->set($key, $rows, ConstantsUtil::TIME_CACHE_7200);  

            }else{
                $rows=$cache;  
            } 
            return $rows;
        }
        public function getGameOfflineHot($currentPage,$numberRecordPerPage)
        {
            $startRecord = ($currentPage - 1) * $numberRecordPerPage;
            $sql = "SELECT id, title,introtext, alias,picture,download,create_date FROM c_game WHERE  isOnline= 0 AND status=1 ORDER BY download DESC LIMIT " . $startRecord . ", " . $numberRecordPerPage;
            $command = Yii::app()->db->createCommand($sql);
            $rows = $command->queryAll();   
            return $rows;
        }

        public function getTotalGameOnline()
        {
            $sql = "SELECT count(id) FROM c_game WHERE  isOnline= 1 AND status=1 ";
            $command = Yii::app()->db->createCommand($sql);
            $rows = $command->queryRow();        
            return $rows["count(id)"];
        }

        public function getTotalGameOffline()
        {
            $sql = "SELECT count(id) FROM c_game WHERE  isOnline= 0 AND status=1 ";
            $command = Yii::app()->db->createCommand($sql);
            $rows = $command->queryRow();        
            return $rows["count(id)"];
        }

        public function getInfoGame($game_id)
        {
            $cacheService = new CacheService("Game","getGameById",$game_id);
            $key = $cacheService->createKey();
            $cache = Yii::app()->cache->get($key);
            if ($cache == false) 
            {
                $connect =Yii::app()->db;
                $sql="SELECT * FROM c_game WHERE id=".$game_id." AND status=1";
                $command=$connect->createCommand($sql);
                $row = $command->queryRow();
                Yii::app()->cache->set($key, $row, ConstantsUtil::TIME_CACHE_7200);
            }
            else
            {
                $row=$cache;
            }
            return $row;
        }
        
        public function getRandomGame($isOnline){
            $connect =Yii::app()->db;
            $sql="SELECT * FROM c_game WHERE isOnline = ".$isOnline." AND status=1 ORDER BY RAND() LIMIT 0,5";
            $command=$connect->createCommand($sql);
            $rows= $command->queryAll();
            return $rows;
        }
    }
?>
